/**
 * Writed by yura and mak
 */

package wz.legend.handlers;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.impl.DefaultHttpRequestFactory;
import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.impl.io.HttpResponseParser;
import org.apache.http.message.BasicLineParser;
import org.apache.http.params.BasicHttpParams;

import wz.legend.ByteArraySessionInputBuffer;
import wz.legend.HttpContext;
import wz.legend.ResponseParser;


public class Util {

    public static HttpContext createHttpContext(String path, String resourceName)
            throws Exception {
        byte[] response = IOUtils.toByteArray(Util.class.getClassLoader()
                .getResourceAsStream(resourceName));

        BasicHttpParams params = new BasicHttpParams();
        HttpResponseParser parser = new ResponseParser(
                new ByteArraySessionInputBuffer(response, params),
                new BasicLineParser(), new DefaultHttpResponseFactory(), params);

        HttpRequest httpRequest = new DefaultHttpRequestFactory()
                .newHttpRequest("GET", path);
        HttpResponse httpResponse = (HttpResponse) parser.parse();

        return new HttpContext(httpRequest, httpResponse);
    }

}
